﻿Imports System.IO
Imports System.Data
Imports ADOEntity

Partial Class Administrator_frmHistoryPurge
    Inherits Web.UI.BasePage
    Private Const strNameFileXml As String = "\\ErrorExplain.xml"
    Private Const strPathFolder As String = "D:\\PurgeDATA\\backup_Log\\"
    Private Const strLog_Login As String = "Log_Login"
    Private Const strLog_Transaction As String = "Log_Transaction"
    Dim ds_Table As New DataSet
    Dim convertDate As clsDateFormat
    Dim clsSessionMenuRight As clsSessionMenuRight

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        lblMsg.Text = Resources.resMessageAlert.alertEmptyGrid
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            clsSessionMenuRight = Session("ClsMenuRight")

            If Not (IsPostBack) Then
                setDefaultSearch()
                CreateControlMP()
            Else
                If Not (ddlTableLog.SelectedValue = -1) Then


                    If (ddlTableLog.SelectedValue = 0) Then
                        ds_Table = Session("dsTblMaster")
                        gvLogLogin.DataSource = ds_Table.Tables("tblSearchLogLogin")
                        gvLogTran.DataSource = Nothing
                        gvLogTran.DataBind()
                        gvLogLogin.DataBind()

                    ElseIf (ddlTableLog.SelectedValue = 1) Then
                        ds_Table = Session("dsTblMaster")
                        gvLogTran.DataSource = ds_Table.Tables("tblSearchLogTran")
                        gvLogTran.DataBind()
                        gvLogLogin.DataSource = Nothing
                        gvLogLogin.DataBind()
                    End If
                Else
                    gvLogLogin.DataSource = Nothing
                    gvLogLogin.DataBind()
                    gvLogTran.DataSource = Nothing
                    gvLogTran.DataBind()
                    lblMsg.Visible = False
                    lblTotal.Visible = False

                End If
            End If
        Catch ex As Exception

        End Try

    End Sub

    Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
        Session("dsTblMaster") = ds_Table
    End Sub
    Public Sub setDefaultSearch()

        convertDate = New clsDateFormat
        txtStartDate.Text = convertDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, dteDateIsChk:=Date.Now)
        txtEndDate.Text = convertDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, dteDateIsChk:=Date.Now)



    End Sub

    Sub CreateControlMP()
        Dim lblForm As Label = CType(Master.FindControl("lblForm"), Label)
        lblForm.Text = "ประวัติการใช้งาน > ประวัติการเก็บไฟล์ Log"
    End Sub

    Public Sub readFileInDirectory(ByVal ddlNameFile As DropDownList, ByVal strFolderName As String)
        Try
            Dim myDirInfo As DirectoryInfo
            Dim arrFolderInfo As Array
            myDirInfo = New DirectoryInfo(strPathFolder & strFolderName & "\\")

            arrFolderInfo = myDirInfo.GetFiles
            Dim AddItem As ListItem = Nothing

            ddlNameFile.Items.Clear()

            AddItem = New ListItem("*** กรุณาเลือก ***", 0)
            ddlNameFile.Items.Insert(0, AddItem)
            For iCountArr = 0 To arrFolderInfo.Length - 1
                AddItem = New ListItem(arrFolderInfo(iCountArr).ToString, arrFolderInfo(iCountArr).ToString)
                ddlNameFile.Items.Insert(iCountArr + 1, AddItem)
            Next


            myDirInfo = Nothing
        Catch ex As Exception

        End Try


    End Sub

    Protected Sub ddlTableLog_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlTableLog.SelectedIndexChanged
        If Not (ddlTableLog.SelectedValue = -1) Then
            If (ddlTableLog.SelectedValue = 0) Then
                readFileInDirectory(ddlFileName, strLog_Login)
            ElseIf (ddlTableLog.SelectedValue = 1) Then
                readFileInDirectory(ddlFileName, strLog_Transaction)
            End If
        Else
            ddlFileName.Items.Clear()
        End If
        
    End Sub

    Protected Sub ddlFileName_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlFileName.SelectedIndexChanged
        Try

            If (ddlFileName.Items.Count = 0) Then
                Exit Sub

            End If
            Dim strPathAll As String = ""
            Dim ds As New DataSet
            If (ddlTableLog.SelectedValue = 0) Then

                If (ds_Table.Tables.Contains("sLoginLog")) Then
                    ds_Table.Tables("sLoginLog").Clear()
                    ds_Table.Tables.Remove("sLoginLog")
                End If

                strPathAll = strPathFolder & strLog_Login & "\\" & ddlFileName.SelectedItem.ToString
                ds.ReadXml(strPathAll)
                ds_Table.Tables.Add(ds.Tables("sLoginLog").Copy)
                gvLogTran.DataSource = Nothing
                ' IsDataBind()
            ElseIf (ddlTableLog.SelectedValue = 1) Then

                If (ds_Table.Tables.Contains("LOG_TRANSACTION")) Then
                    ds_Table.Tables("LOG_TRANSACTION").Clear()
                    ds_Table.Tables.Remove("LOG_TRANSACTION")
                End If

                strPathAll = strPathFolder & strLog_Transaction & "\\" & ddlFileName.SelectedItem.ToString

                ds.ReadXml(strPathAll)
                ds_Table.Tables.Add(ds.Tables("LOG_TRANSACTION").Copy)
                gvLogLogin.DataSource = Nothing

                'IsDataBind()
            End If
        Catch ex As Exception

        End Try
    End Sub

#Region "gvLogLogin"

    Protected Sub gvLogLogin_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvLogLogin.RowDataBound
        Try

            convertDate = New clsDateFormat
            If (e.Row.RowType.ToString = "DataRow") Then
                If Not (e.Row.Cells(3).Text = Nothing) Then
                    Dim dteDateLogin As Date = e.Row.Cells(3).Text
                    Dim strDate As String = ""
                    strDate = convertDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, strDateIsChk:=dteDateLogin.ToShortDateString)
                    strDate = strDate & " " & dteDateLogin.ToLongTimeString
                    e.Row.Cells(3).Text = strDate

                End If
                If Not (e.Row.Cells(3).Text = Nothing) Then
                    Dim strTranType As String = e.Row.Cells(2).Text
                    e.Row.Cells(2).Text = If(strTranType = 0, "ออกจากระบบ", If(strTranType = 1, "เข้าใช้ระบบ", "เข้าใช้งานไม่สำเร็จ"))
                End If
            End If

        Catch ex As Exception
            WriteError(ex, "gvLogLogin_RowDataBound", "frmHistoryPurge")
        End Try
    End Sub

    Protected Sub gvLogLogin_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvLogLogin.PageIndexChanging
        gvLogLogin.PageIndex = e.NewPageIndex

        DataBind()
    End Sub


#End Region

#Region "gvLogTran"

    Protected Sub gvLogTran_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvLogTran.RowDataBound
        Try
            Dim convertDate As clsDateFormat
            convertDate = New clsDateFormat
            If (e.Row.RowType.ToString = "DataRow") Then
                If Not (e.Row.Cells(4).Text = Nothing) Then
                    Dim dteDateLogin As Date = e.Row.Cells(4).Text
                    Dim strDate As String = ""
                    strDate = convertDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, strDateIsChk:=dteDateLogin.ToShortDateString)
                    strDate = strDate & " " & dteDateLogin.ToLongTimeString
                    e.Row.Cells(4).Text = strDate
                End If
                If Not (e.Row.Cells(2).Text = Nothing) Then
                    Dim strTranType As String = e.Row.Cells(2).Text
                    e.Row.Cells(2).Text = If(strTranType = 1, "ค้นหาข้อมูล", If(strTranType = 2, "พิมพ์ฟอร์ม", If(strTranType = 3, "บันทึึก ข้อมูล", "บันทึก แก้ไข")))
                End If
            End If
        Catch ex As Exception
            WriteError(ex, "gvLogLogin_RowDataBound", "frmTran")
        End Try
    End Sub

    Protected Sub gvLogTran_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvLogTran.PageIndexChanging
        gvLogTran.PageIndex = e.NewPageIndex
        DataBind()
    End Sub

#End Region

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        IsDataBind()


    End Sub

    Private Sub IsDataBind()
        Try

       


        Dim clsLinqToDataset As New clsLINQToDataTable
        Dim UserEntity As New UserEntities
        If (ds_Table.Tables.Contains("tblSearchLogTran")) Then
            ds_Table.Tables("tblSearchLogTran").Clear()
            ds_Table.Tables.Remove("tblSearchLogTran")
        End If

        If (ds_Table.Tables.Contains("tblSearchLogLogin")) Then
            ds_Table.Tables("tblSearchLogLogin").Clear()
            ds_Table.Tables.Remove("tblSearchLogLogin")
        End If

        Dim clsFormatDate As New clsDateFormat
        If (ddlTableLog.SelectedValue = 0) Then

            Dim tbl = From tblLogLogin In ds_Table.Tables("sLoginLog").AsEnumerable()
                     Select userName = tblLogLogin("USERNAME"), EmpName = tblLogLogin("EMPNAME"), EmpLastName = tblLogLogin("EMPLASTNAME"),
                     LoginType = tblLogLogin("LOGINTYPE"), LoginDate = tblLogLogin("LOGINDATE"), LastIP = tblLogLogin("LASTIP"),
                     DEPTCODE = tblLogLogin("DEPTCODE"), Branchcode = tblLogLogin("BRANCHCODE")




            Dim tbljoin = From tblLog_Login In tbl Group Join tbl_Branch In UserEntity.SBRANCH On tblLog_Login.Branchcode Equals tbl_Branch.BRANCHCODE Into tblLeftUr_br = Group
            From tbluserLeftBr In tblLeftUr_br.DefaultIfEmpty Group Join tbl_Dept In UserEntity.SDEPARTMENT On tblLog_Login.DEPTCODE Equals tbl_Dept.DEPTCODE Into tblLeftDept = Group
            From tblLeftBr_Dept In tblLeftDept.DefaultIfEmpty()
            Select tblLog_Login.userName, tblLog_Login.LoginType, tblLog_Login.LoginDate, tblLog_Login.LastIP, tblLog_Login.EmpName,
            tblLog_Login.EmpLastName, tblLeftBr_Dept.DEPTNAME, tbluserLeftBr.BRANCHNAME

            If Not (txtUserName.Text = "") Then
                tbljoin = tbljoin.Where(Function(LogLogin) LogLogin.userName.ToString.Contains(txtUserName.Text.Trim))
            End If

            If Not (txtStartDate.Text = "" And txtEndDate.Text = "") Then
                Dim strStartDate As Date = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtStartDate.Text) & " 00:00:00"
                Dim strEndDate As Date = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtEndDate.Text) & " 23:59:59"
                tbljoin = tbljoin.Where(Function(LogLogin) LogLogin.LoginDate >= strStartDate And LogLogin.LoginDate <= strEndDate)
            End If
            ds_Table.Tables.Add(clsLinqToDataset.LINQToDataTable(tbljoin, "tblSearchLogLogin"))




            gvLogLogin.DataSource = ds_Table.Tables("tblSearchLogLogin")
            gvLogLogin.DataBind()
            countRow()
        ElseIf (ddlTableLog.SelectedValue = 1) Then

            Dim tbl = From tblLOG_TRANSACTION In ds_Table.Tables("LOG_TRANSACTION").AsEnumerable()
                      Select UNIQUEID = tblLOG_TRANSACTION("UNIQUEID"), UPDATEBY = tblLOG_TRANSACTION("UPDATEBY"),
                      TRANTYPE = tblLOG_TRANSACTION("TRANTYPE"), DESCRIPTION = tblLOG_TRANSACTION("DESCRIPTION"),
                      LOGDATE = tblLOG_TRANSACTION("LOGDATE")

            If Not (txtUserName.Text = "") Then
                tbl = tbl.Where(Function(LogTran) LogTran.UPDATEBY.ToString.Contains(txtUserName.Text.Trim))
            End If

            If Not (txtStartDate.Text = "" And txtEndDate.Text = "") Then
                Dim strStartDate As Date = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtStartDate.Text) & " 00:00:00"
                Dim strEndDate As Date = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtEndDate.Text) & " 23:59:59"

                tbl = tbl.Where(Function(LogTran) LogTran.LOGDATE >= strStartDate And LogTran.LOGDATE <= strEndDate)
            End If


            ds_Table.Tables.Add(clsLinqToDataset.LINQToDataTable(tbl, "tblSearchLogTran"))

            gvLogTran.DataSource = ds_Table.Tables("tblSearchLogTran")
            gvLogTran.DataBind()
            countRow()
        End If



        Catch ex As Exception
            WriteError(ex, "IsDataBind", "frmHistoryPurge")
        End Try
    End Sub

    Private Sub countRow()
        If (ddlTableLog.SelectedValue = 0) Then
            If (ds_Table.Tables("tblSearchLogLogin").Rows.Count = 0) Then
                lblMsg.Visible = True
                lblTotal.Visible = False
            Else
                lblMsg.Visible = False
                lblTotal.Visible = True
                lblTotal.Text = "จำนวนรายการทั้งหมด " & ds_Table.Tables("tblSearchLogLogin").Rows.Count & " รายการ"
            End If

        ElseIf (ddlTableLog.SelectedValue = 1) Then

            If (ds_Table.Tables("tblSearchLogTran").Rows.Count = 0) Then
                lblMsg.Visible = True
                lblTotal.Visible = False
            Else
                lblMsg.Visible = False
                lblTotal.Visible = True
                lblTotal.Text = "จำนวนรายการทั้งหมด " & ds_Table.Tables("tblSearchLogTran").Rows.Count & " รายการ"
            End If

        End If

    End Sub

    Private Sub SaveLogSearch()
        Try

            Dim desc As String = ""

            If Not (ddlTableLog.SelectedIndex = 0) Then
                desc += " ชื่อ Log - " + ddlTableLog.SelectedItem.Text
            End If

            If Not (ddlFileName.SelectedIndex = 0) Then
                desc += " ชื่อไฟล์ - " + ddlFileName.SelectedItem.Text
            End If

            If Not (txtUserName.Text = "") Then
                desc += " รหัสผู้ใช้งาน - " + txtUserName.Text
            End If

            If Not (txtStartDate.Text = "") And Not (txtEndDate.Text = "") Then
                desc += " วันที่ตั้งแต่ - " + txtStartDate.Text & " ถึง " & txtEndDate.Text
            End If


            Dim clsLogTransaction As New clsLogTransaction
            clsLogTransaction.saveLogTransaction("", clsSessionMenuRight.getUserName, 1, desc, "M72")
            clsLogTransaction = Nothing
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

End Class
